import {
	defineStore
} from 'pinia'

import CONFIG from '@/common/config.js'
import test from '@/common/base/test.js'


// #ifdef H5

// #endif

export default defineStore('data', {
	state: () => ({
		userInfo: uni.getStorageSync('userInfo') || {},
		studentList: uni.getStorageSync('studentList'),
		// BalanceList: uni.getStorageSync('studentList'),
	}),
	getters: {
		getUserInfo: (state) => state.userInfo,
	},
	actions: {
		//动账数据列边
		BalanceChange(form_data, type, money) {
			//学生列表
			let getstudentList = uni.getStorageSync('studentList')
			let getBalanceList = uni.getStorageSync('BalanceList')
			//学生INDEX
			let findIndex = getstudentList.findIndex((item) => item.id == form_data.id);
			//当前学生数据
			let {
				name,
				id,
				balance
			} = getstudentList[findIndex]

			//本次动账数据
			// *姓名 id  *卡内余额 动账金额 动账方式  时间   体育项目 备注
			let newitem = {
				name,
				id,
				balance: type == 1 ? Number(balance) + Number(form_data.money) : Number(balance) - form_data
					.money,
				type,
				ctime: form_data.ctime,
				money: form_data.money,
				note: form_data.note,
				typeText: form_data.typeText,
			}
			//当前学生数据余额变动
			getstudentList[findIndex].balance = newitem.balance
			uni.setStorageSync('studentList', getstudentList)
			this.studentList = getstudentList
			if (getBalanceList) {
				getBalanceList.unshift(newitem)
				uni.setStorageSync('BalanceList', getBalanceList)
				// this.BalanceList = getBalanceList
			} else {
				uni.setStorageSync('BalanceList', [newitem])
				this.studentList = getstudentList
				// this.BalanceList = [newitem]
			}
			console.log(this.studentList);
			// console.log(this.BalanceList);
		},
		//删除学院信息
		deletItemTostudentList(form_data){
			let getStore = uni.getStorageSync('studentList')
			let findIndex = getStore.findIndex((item) => item.id == form_data.id);
			if(findIndex>-1){
				getStore.splice(findIndex,1)
				uni.setStorageSync('studentList', getStore)
				this.studentList = getStore
			}
			
		},
		// 修改学生信息
		editItemTostudentList(form_data) {
			let getStore = uni.getStorageSync('studentList')
			let findIndex = getStore.findIndex((item) => item.id == form_data.id);
			getStore[findIndex] = form_data
			uni.setStorageSync('studentList', getStore)
			this.studentList = getStore
		},
		// 添加学生信息
		addItemTostudentList(e) {
			let getStore = uni.getStorageSync('studentList')
			if (getStore) {
				getStore.unshift(e)
				uni.setStorageSync('studentList', getStore)
				this.studentList = getStore
			} else {
				uni.setStorageSync('studentList', [e])
				this.studentList = [e]
			}
		},
		setCurrentAddressInfo(info) {
			this.currentAddressInfo = info
			uni.setStorageSync("currentAddressInfo", info)
		},
		setUserInfo(userInfo) {
			this.userInfo = userInfo
		},
		resetUserInfo(info = {}) {
			this.userInfo = {}
			uni.setStorageSync('userInfo', {})
		},

	}
})